﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.InputOut;
using CE.CarInsurance;
using CE.Mail;

public partial class carinsurance_renewal_renewal_notice_list : System.Web.UI.Page
{
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["tab"] == null)
            return;

        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "201")
        {
            Response.Redirect("~/carinsurance/PendingApplication.aspx");
        }

        if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "0")
        {
            Response.Redirect("~/SiteUserHome.aspx");
        }

        if (!IsPostBack)
        {
            hidAgentId.Value = Session[CE.Global.General.UserId].ToString();
            LoadGrid();
        }
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        
        DataTable _dtSummary = new DataTable();
        int tab = int.Parse(Request.QueryString["tab"]);
        int mth = 0;
        if (tab == 1)
            mth = -2;
        else if (tab == 2)
            mth = -1;
        else if (tab == 3)
            mth = 0;
        else if (tab == 4)
            mth = 1;
        else if (tab == 5)
            mth = 2;
        else if (tab == 6)
            mth = 3;
        string _ReportDate1 = dateTimeNow.AddMonths(mth).ToString("yyyyMMdd");
        
        MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
        string _spName = "[sprocCXCRMNonGGRenewalLeadList]";
        SortedList _sPara = new SortedList();

        _sPara.Add("@ReportDate", _ReportDate1);
        _sPara.Add("@UserID", Session[CE.Global.General.UserId].ToString());

        _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
        if (_dtSummary != null && _dtSummary.Rows.Count > 0)
        {
            lblError.Text = "";

            object rTotal = _dtSummary.Compute("SUM(RenewPremium)", "");
            if (rTotal != null)
                lblRenewTotal.Text = decimal.Parse(rTotal.ToString()).ToString("#,##0.00");

            DataView dv = new DataView();
            dv = new DataView(_dtSummary);

            //if (ddlStatus.SelectedItem.Value == "1")
            //{
            //    //Call Now
            dv.RowFilter = "ISNULL(NewRefNo,'') = '' ";
            //}
            //else if (ddlStatus.SelectedItem.Value == "2")
            //{
            //    //Close
            //    dv.RowFilter = "CallFinishType = '100' OR ISNULL(NewRefNo,'') <> '' ";
            //}
            dv.Sort = "CoverageStart";
            grvApplication.DataSource = dv;
            grvApplication.DataBind();
        }
        else
        {
            lblRenewTotal.Text = "";

            grvApplication.DataSource = null;
            grvApplication.DataBind();
        }
    }

    protected void grvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal lblCallStatusDesc = (Literal)e.Row.FindControl("lblCallStatusDesc");
            string callType = DataBinder.Eval(e.Row.DataItem, "CallFinishType").ToString();
            string saleComplete = e.Row.Cells[7].Text;

            HyperLink linkCall = (HyperLink)e.Row.FindControl("linkCallNow");
            if (saleComplete.Trim() != "&nbsp;" || callType == "100")
            {
                linkCall.Enabled = false;
                linkCall.Text = Resources.General.Closed;
                //LinkButton linkbutton = (LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0]; //10
                //linkbutton.Enabled = false;
                //linkbutton.Text = Resources.General.Closed;
            }
            else
            {
                string LastYearRefNo = e.Row.Cells[0].Text;

                Int64 _customerId = 0;
                if (!Int64.TryParse(e.Row.Cells[5].Text, out _customerId))
                {
                    _customerId = 0;
                }

                Int64 _carId = 0;
                if (!Int64.TryParse(e.Row.Cells[6].Text, out _carId))
                {
                    _carId = 0;
                }

                Int64 _agentID = 0;
                if (!Int64.TryParse(Session[CE.Global.General.UserId].ToString(), out _agentID))
                {
                    _agentID = 0;
                }

                //../../carinsurance/renewal/
                linkCall.NavigateUrl = "renewal/renew-check.aspx?custId=" + _customerId.ToString() +
                    "&carId=" + _carId.ToString() + "&refno=" + LastYearRefNo; //+ "&agentId=" + _agentID.ToString()
            }

            switch (callType)
            {
                case "1":
                    lblCallStatusDesc.Text = "<span style='color:red;'>" + Resources.General.CallFinish + "</span>";
                    lblCallStatusDesc.Text += "<hr style='border: 1px solid #E6E6E6; bor' />";
                    break;
                case "2":
                    lblCallStatusDesc.Text = Resources.General.Callback + " <span style='color:red;'>[" + Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "CallBackDateTime").ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "]</span>";
                    lblCallStatusDesc.Text += "<hr style='border: 1px solid #E6E6E6; bor' />";
                    break;
                case "3":
                    lblCallStatusDesc.Text = "<span style='color:green;'>" + Resources.General.FollowUp + "</span>";
                    lblCallStatusDesc.Text += "<hr style='border: 1px solid #E6E6E6; bor' />";
                    break;
                default:
                    lblCallStatusDesc.Text = "";
                    break;
            }

            int tab = int.Parse(Request.QueryString["tab"]);
            int mth = 0;
            if (tab == 1)
                mth = -2;
            else if (tab == 2)
                mth = -1;
            else if (tab == 3)
                mth = 0;
            else if (tab == 4)
                mth = 1;
            else if (tab == 5)
                mth = 2;
            else if (tab == 6)
                mth = 3;
            string _ReportDate1 = dateTimeNow.AddMonths(mth).ToString("yyyy/MM");

            string sharedlink = "http://zebra/cx/";
            string cName = e.Row.Cells[1].Text;
            string cPlate = e.Row.Cells[2].Text;
            string cDate = e.Row.Cells[3].Text;
            if (cDate.Trim() != "")
            {
                HyperLink linkPdf = (HyperLink)e.Row.FindControl("linkPdf");
                //string[] path = cDate.Split('/');
                linkPdf.Text = "Pdf";
                if (cPlate.Trim() != "ป้ายแดง")
                {
                    linkPdf.NavigateUrl = sharedlink + _ReportDate1 + "/" + cPlate.Trim() + ".pdf";
                }
                else
                {
                    cName = cName.Trim().Replace(" ", "%20");
                    linkPdf.NavigateUrl = sharedlink + _ReportDate1 + "/" + cName.Trim() + ".pdf";
                }
            }

        }
    }

}
